<template>
    <div>
        <Modal v-model="modal" class="modal-full modal-form" :title="modalTitle" :width="modalWidth">
            <div v-if="modalShow">
                <Spin size="large" fix v-show="!modelLoad"></Spin>
                <Form-control @formInput="enableSubmit=true" v-if="modelLoad" @validate="validateChange" @build="formBuild" :initData="defaultData" :module="actionModel.code+'_'+'form'" ref="form" :col="$parent.modelParam('col')||1" v-model="data" :model="formModel"></Form-control>
            </div>
            <div slot="footer" class="button-panel-button">
                <i-button @click="close">取消</i-button>
                <i-button @click="formSubmit" :loading="posting && false" :disabled="!enableSubmit && false" type="primary">确认</i-button>
            </div>
        </Modal>
    </div>
</template>  
 
<script>
import Base from "../modalBase";
import FormControl from "components/form";
import $ from "@/common";
import { billParser } from "@/common/parser";

const sizeWidth = {
  s: 400,
  m: 800,
  l: 900,
  max: "90%"
};
export default {
  extends: Base,
  props: {},
  data() {
    return {
      //使用所有者的model作为模型
      path: {
        bill_submit: "/service/{app}/bill/data/{bill_code}",
        bill_find: "/service/{app}/bill/data/{bill_code}?id={id}",
        bill_model: "/service/{app}/bill/model/{bill_code}",
        bill_def: "/service/{app}/bill/model/{bill_code}/def_value"
      },
      //扩展model加载中
      modelLoad: true,
      ownerForm: true,
      modelExt: {},
      formModel: {},
      //默认数据
      defaultData: null,
      //首次加载状态 用于判断是否加载过model
      fristLoad: true,
      isValidate: true,
      //模型为标准的单据模板，支持默认处理
      isBillModel: true,
      //模型是否为本地文件数据，本地数据将不会通过远程链接取得默认数据
      isLocalModel: false,
      posting: false,
      data: {},
      tempData: {},
      modelPath: null,
      dataLoading: false
    };
  },
  computed: {
    modalWidth() {
      var size = this.$parent.modelParam("size");
      if (!size) {
        if (this.formModel.slaves && this.formModel.slaves.length) {
          if (this.formModel.slaves.length > 6) {
            size = "max";
          } else {
            size = "l";
          }
        } else {
          size = "m";
        }
      }
      return sizeWidth[size];
    },
    modalTitle() {
      var name = (this.formModel && this.formModel.name) || "数据";
      return `${name}明细`;
    }
  },
  watch: {},
  created() {
    this.getFormModel();
  },
  methods: {
    load() {
      this.isValidate = true;
    },
    updateData() {},
    formBuild() {
      this.$refs.form && (this.$refs.form.$parentAction = this);
    },
    close() {
      this.modal = false;
    },
    getFormModel() {
      //从所有者元素处获得模型
      var modelCode = this.modelParam("model");
      if (this.ownerForm) {
        this.formModel = this.$owner.model;
        this.billCode = this.$owner.model.bill_code;
        this.isBillModel = true;
        this.isLocalModel = false;
      }
      if (this.modelParam("local")) {
        this.isLocalModel = true;
      }
      //获取表单扩展模型
      if (modelCode) {
        this.modelLoad = false;
        this.modelLoad = true;
        this.isBillModel = false;
        require(["./model/" + modelCode], data => {
          var modelExt = data.default;
          this.isLocalModel = true;
          this.formModel = modelExt.getModel(this);
        }).catch(ex => {});
      } else if (
        this.modelParam("bill_code") ||
        this.modelParam("path_model")
      ) {
        //通过参数指定路径查询model
        this.modelLoad = false;
        this.billCode = this.modelParam("bill_code");
        this.isLocalModel = false;
        let path = this.modelParam("path_model");
        if (path) {
          this.isBillModel = false;
        } else {
          this.isBillModel = true;
          path = this.path.bill_model;
        }
        this.modelPath = $.composePath(path, {
          ...this.actionData,
          bill_code: this.billCode
        });
        $.link(this.modelPath + "?enable_access_token=true", this.actionData)
          .then(res => {
            this.isBillModel = true;
            this.tempData = res.data.data;
            this.formModel = billParser(res.data);
            //设置初始数据
            if (res.data.data) this.formModel.data = res.data.data;
            this.modelLoad = true;
          })
          .catch(() => {
            this.close();
          });
      } else if (this.modelParam("slave_code")) {
        //指定本地model的子表操作
        this.isLocalModel = true;
        var slaveCode = this.modelParam("slave_code");
        this.formModel.slaves.forEach(slave => {
          if (slave.code === slaveCode) {
            this.formModel = slave;
          }
        });
      }
    },
    formSubmit() {
      this.$refs.form.doValidate();
      this.$nextTick(() => {
        if (!this.enableSubmit && 0) {
          this.$Message.error("表单验证失败!");
        } else {
          this.actionSubmit();
        }
        this.isValidate = true;
      });
    },
    actionSubmit() {
      return this.putData();
    },
    validateChange(v) {
      this.enableSubmit = v;
    },
    putData() {
      var data = this.$refs.form.getData
        ? this.$refs.form.getData()
        : this.$refs.form.formData;
      if (this.$owner.model.bill_code && this.$store.state.user) {
        data.tenantry_id = this.$store.state.user.current_tenantry.id;
      }
      this.enableSubmit = false;
      this.posting = true;
      $.link(this.getSubmitPath(), data, {
        method: "put"
      })
        .then(res => {
          //this.close();
          this.$parent.actionSuccess();
          this.$owner.update();
        })
        .finally(() => {
          this.posting = false;
          this.enableSubmit = true;
        });
    },
    getSubmitPath() {
      var path =
        this.modelParam("pathSubmit") || this.modelParam("path_submit");
      if (!path) {
        path = $.composePath(this.path.bill_submit, {
          bill_code: this.modelParam("bill_code") || this.$owner.model.bill_code
        });
      }
      return path;
    },
    getFindPath() {
      var path = this.modelParam("pathFind") || this.modelParam("path_find");
      if (!path) {
        path = $.composePath(this.path.bill_find, {
          bill_code: this.modelParam("bill_code") || this.$owner.model.bill_code
        });
      }
      return path;
    }
  },
  components: {
    FormControl
  }
};
</script>

<style lang="less">
.ivu-modal-body {
  .ivu-tabs-content {
    margin-top: -8px;
  }
}
.modal-form .ivu-modal-header,
.modal-form .ivu-modal-footer {
  z-index: 9;
  position: relative;
}
</style>
